package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Music;
import com.qf.service.Impl.MusicServiceImpl;
import com.qf.service.MusicService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 音乐找到所有servlet
 *
 * @author 阿亘
 * @date 2023/04/20
 */
@WebServlet("/musicFindByIdServlet.action")
public class MusicFindByIdServlet extends HttpServlet {
    private MusicService musicService = new MusicServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前端传过来的参数
        String musicId = req.getParameter("musicId");
        //判断
        if (musicId != null){
            //调用Service层模糊查询
            Music music = musicService.findById(musicId);
            //类型转换，转成Json类型
            String jsonMusicList = JSON.toJSONString(music);
            //返回给前端
            resp.getWriter().write(jsonMusicList);
        }else {
            throw new RuntimeException("歌曲id不能为空");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
